package com.qk.management.service.serviceimpl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.domain.CourseDO;
import com.qk.dto.course.CourseDTO;
import com.qk.management.mapper.CourseMapper;
import com.qk.management.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author 上官徵羽
 * @version 1.0
 * @since 2025-10-11 21:06
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, CourseDO> implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    @Override
    public PageResult<CourseDO> listPage(CourseDTO courseDTO) {
        Page<CourseDO> page = new Page<>(courseDTO.getPage(), courseDTO.getPageSize());
        LambdaQueryWrapper<CourseDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtil.isEmpty(courseDTO.getName())){
            lambdaQueryWrapper.like(CourseDO::getName, courseDTO.getName());
        }
        if (!ObjectUtil.isEmpty(courseDTO.getSubject())){
            lambdaQueryWrapper.eq(CourseDO::getSubject, courseDTO.getSubject());
        }
        if (!ObjectUtil.isEmpty(courseDTO.getTarget())){
            lambdaQueryWrapper.eq(CourseDO::getTarget, courseDTO.getTarget());
        }
        Page<CourseDO> pageResult = courseMapper.selectPage(page, lambdaQueryWrapper);
        return PageResult.<CourseDO>builder()
                .total(pageResult.getTotal())
                .rows(pageResult.getRecords())
                .build();
    }
}
